<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('文章列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>标题：</label>
                            <input type="text" name="title"/>
                        </li>
                        <li>
                            <label>作者：</label>
                            <input type="text" name="author"/>
                        </li>
                        <!--                            <li>-->
                        <!--                                <label>性别</label>-->
                        <!--                                <select name="sex" th:with="type=${@dict.getType('sys_user_sex')}">-->
                        <!--                                    <option value="">所有</option>-->
                        <!--                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>-->
                        <!--                                </select>-->
                        <!--                            </li>-->
                        <!--                        <li>-->
                        <!--                            <label>创建时间：</label>-->
                        <!--                            <input type="text" name="creatTime"/>-->
                        <!--                        </li>-->
                        <li>
                            <label>关键词：</label>
                            <input type="text" name="keyword"/>
                        </li>
                        <!--                        <li>-->
                        <!--                            <label>审核状态</label>-->
                        <!--                            <input type="text" name="isAudit"/>-->
                        <!--                        </li>-->
                        <li>
                            <label>用户部门：</label>
                            <input type="text" name="dept"/>
                        </li>

                        <li>
                            文章类型：<select name="type" th:with="type=${@dict.getType('sys_kow_type')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                        </li>


                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.addFull()" shiro:hasPermission="system:knowledgepool:add">
                <i class="fa fa-plus"></i> 添加知识
            </a>
            <!--                <a class="btn btn-success" onclick="$.operate.addforsum()" shiro:hasPermission="system:knowledgepool:add">-->
            <!--                    <i class="fa fa-plus"></i> 添加文件-->
            <!--                </a>-->

            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="system:knowledgepool:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:knowledgepool:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()"
               shiro:hasPermission="system:knowledgepool:export">
                <i class="fa fa-download"></i> 导出
            </a>

            <!--                <a class="btn btn-success" onclick="$.operate.addforsum()" shiro:hasPermission="system:knowledgepool:addforsum">-->
            <!--                    <i class="fa fa-plus"></i> 创建文章-->
            <!--                </a>-->


        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    /* Thymeleaf variables */
    var editFlag = /*[[${@permission.hasPermi('system:knowledgepool:edit')}]]*/ false;
    var removeFlag = /*[[${@permission.hasPermi('system:knowledgepool:remove')}]]*/ false;
    var downloadFlag = /*[[${@permission.hasPermi('system:knowledgepool:download')}]]*/ false;
    var sexDatas = /*[[${@dict.getType('sys_user_sex')}]]*/ [];
    var isAuditDatas = /*[[${@dict.getType('sys_notice_status')}]]*/ [];
    var iskowType = /*[[${@dict.getType('sys_kow_type')}]]*/ [];
    var prefix = ctx + "system/knowledgepool";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            downloadUrl: prefix + "/download",
            modalName: "文章",
            sortName: "creatTime",
            sortOrder: "desc",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'keyword',
                    title: '关键词'
                },
                {
                    field: 'title',
                    title: '标题'
                },
                // {
                //     field: 'content',
                //     title: '内容',
                //     formatter: function (value, row, index) {
                //         var span=document.createElement('span');
                //         span.setAttribute('title',value);
                //         span.innerHTML = value;
                //         return span.outerHTML;
                //     }
                // },
                {
                    field: 'commentCount',
                    title: '评论数',
                    sortable: true
                },
                {
                    field: 'collectionCount',
                    title: '收藏数',
                    sortable: true
                },
                {
                    field: 'author',
                    title: '作者'
                },
                {
                    field: 'uid',
                    title: '作者id',
                    visible: false
                },
                {
                    field: 'sex',
                    title: '性别',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(sexDatas, value);
                    }
                },
                {
                    field: 'creatTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    field: 'lastTime',
                    title: '修改时间'
                },

                // {
                //     field: 'isAudit',
                //     title: '审核',
                //     formatter: function (value, row, index) {
                //         return $.table.selectDictLabel(isAuditDatas, value);
                //     }
                // },
                {
                    field: 'likeCount',
                    title: '点赞数',
                    sortable: true
                },
                {
                    field: 'pageViews',
                    title: '浏览数',
                    sortable: true
                },
                {
                    field: 'userPower',
                    title: '用户权限',
                    visible: false
                },
                {
                    field: 'dept',
                    title: '文章来源'
                },
                {
                    field: 'permissionType',
                    title: '文章类别',
                    visible: true
                },
                {
                    field: 'type',
                    title: '类型',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(iskowType, value);
                    }
                },
                {
                    field: 'firstPinYin',
                    title: '姓名首字母',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        if (row.type === '2') {
                            actions.push('<span>&nbsp&nbsp</span><a class="btn btn-info btn-xs ' + downloadFlag + '" onclick="showFileDownloadOptions(\'' + row.id + '\')"><i class="fa fa-download"></i>下载</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 修改多文件下载功能
    function showFileDownloadOptions(id) {
        // 通过后端接口获取文件列表
        $.ajax({
            url: prefix + "/fileId",
            type: "get",
            data: {
                "kid": id
            },
            success: function (result) {
                // 假设result直接是文件列表数组
                var files = result;
                var options = '';

                if (files && files.length > 0) {
                    for (var i = 0; i < files.length; i++) {
                        var file = files[i];
                        var fileName = file.fileName || file.originalFilename || "文件" + (i + 1);
                        options += '<li style="padding: 5px 0;"><a href="javascript:void(0)" onclick="downloadFileById(\'' + file.id + '\', \'' + fileName + '\')">' + fileName + '</a></li>';
                    }

                    layer.open({
                        type: 1,
                        title: '选择要下载的文件',
                        area: ['350px', 'auto'],
                        content: '<div style="padding: 15px;"><ul style="list-style: none; padding-left: 0;">' + options + '</ul></div>',
                        shadeClose: true
                    });
                } else {
                    $.modal.alertWarning("没有找到可下载的文件");
                }
            },
            error: function () {
                $.modal.alertError("获取文件列表失败");
            }
        });
    }

    // 文件下载
    function appendixDownLoad(id) {
        console.log(id)
        const url = prefix + "/appendixDownLoad/" + id;
        window.open(url);
    }

    // 添加缺失的downloadFileById函数
    function downloadFileById(fileId, fileName) {
        console.log("下载文件ID:", fileId, "文件名:", fileName);
        // 更新下载接口，使用新的后端实现
        const url = prefix + "/appendixDownLoad?id=" + fileId;
        window.open(url);
    }

    // // 保留原来的下载函数作为备用
    // function downloadFile(fileUrl) {
    //     var a = document.createElement('a');
    //     a.style.display = 'none';
    //     a.href = fileUrl;
    //     a.download = fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
    //     document.body.appendChild(a);
    //     a.click();
    //     document.body.removeChild(a);
    // }

</script>

<!-- 操作列 -->
<script id="operateTpl" type="text/html">
    <a class="btn btn-xs btn-primary" onclick="$.operate.edit('{{d.id}}')"><i class="fa fa-edit"></i>编辑</a>
    <a class="btn btn-xs btn-danger" onclick="$.operate.remove('{{d.id}}')"><i class="fa fa-remove"></i>删除</a>
    {{# if(d.fileData && d.fileData.id){ }}
    <a class="btn btn-xs btn-info" onclick="appendixDownLoad('{{d.fileData.id}}')"><i
            class="fa fa-download"></i>下载</a>
    {{# } }}
</script>
</body>
</html>